package com.tour.controller;

import com.tour.bean.*;
import com.tour.service.ScenicSpotService;
import com.tour.service.UserMessageService;
import com.tour.service.UserScenicService;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ScenicSpotController
 * @Author 张梦兴
 * @Date 2020/3/9 21:54
 * @describe
 */
@RestController
@RequestMapping(value = "/ScenicSpot")
public class ScenicSpotController {

    private Logger log=Logger.getLogger(ScenicSpotController.class);

    @Autowired
    private ScenicSpotService service;

    @Autowired
    private UserScenicService userScenicService;
    @Autowired
    private UserMessageService userMessageService;


    /**
     * [查询] 根据主键 id 查询
     * @author 张梦兴
     * @date 2020/03/09
     **/
    @RequestMapping("/load/{id}")
    public Object load(@PathVariable("id") int id){
        return service.load(id);
    }

    /**
     * [查询] 分页查询
     * @author 张梦兴
     * @date 2020/03/09
     *@RequestParam(required = false, defaultValue = "0" ,value="offset") int offset,
     *                                         @RequestParam(required = false, defaultValue = "10", value="pagesize") int pagesize
     * @return
     * */
    @RequestMapping("/pageList")
    public Map<String, Object> pageList(@RequestBody Map<String,Object> map) {
        int offset= (int) map.get("offset");
        int pagesize= (int) map.get("pagesize");
        String title="";
        if(map.containsKey("title")){
            title= (String) map.get("title");
        }
        String star="";
        if(map.containsKey("star")){
            star= (String) map.get("star");
        }
        log.debug("offset="+offset+"--pagesize=:"+pagesize);
        return service.pageList(offset, pagesize,title,star);
    }

    /**
     * [新增] 用户订购景点
     * @author
     * @date 2020/03/15
     **/
    @RequestMapping("/insert/{id}")
    public ReturnT<String> insert(@PathVariable("id") int id, HttpServletRequest request){
        Consumer userInfo = (Consumer) request.getSession().getAttribute("userInfo");
        if(null==userInfo){
            return new ReturnT<>(401, "请先登录！");
        }
        UserScenic byid = userScenicService.getByid(userInfo.getId(), id);
        if(null!=byid){
            return new ReturnT<>(402, "已经存在！");
        }
        UserScenic us=new UserScenic(userInfo.getId(), id);
        userScenicService.insert(us);
        return ReturnT.SUCCESS;
    }

    /**
     * 加载留言
     * @param id
     * @return
     */
    @RequestMapping("/loadMsg/{id}")
    public ReturnT<List<Map>> loadMsg(@PathVariable("id") int id){
        List<Map<String, Object>> mapList = userMessageService.getMsgByModel("S", id);
        ReturnT<List<Map>> result=new ReturnT(mapList);
        return result;
    }

    /**
     * [新增] 用户留言
     * @author
     * @date 2020/03/18
     **/
    @RequestMapping("/insertMsg")
    public ReturnT<String> insertMsg(@RequestBody Map<String,String> map, HttpServletRequest request){
        Consumer userInfo = (Consumer) request.getSession().getAttribute("userInfo");
        if(null==userInfo){
            return new ReturnT<>(401, "请先登录！");
        }
        log.debug("留言信息为："+map);
        if (map.containsKey("id")&&map.containsKey("message")) {
            String message=map.get("message");
            if (StringUtils.isEmpty(message)){
                return new ReturnT<>(402, "缺失必要参数");
            }
            UserMessage msg=new UserMessage();
            msg.setModelId(Integer.parseInt(map.get("id")));
            msg.setMsgModel("S");
            msg.setCurrTime(new Date());
            msg.setUserId(userInfo.getId());
            msg.setMsgContent(message);
            msg.setIsReply("0");
            userMessageService.insert(msg);
        }else {
            return new ReturnT<>(402, "缺失必要参数");
        }

        return ReturnT.SUCCESS;
    }

}
